import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import http from "@/api/http.js";
import http5g from "@/api/http5g.js";
import {httpdong} from "@/api/httpdong.js";
import axios from 'axios'
// 引入JSSDK
import jssdk from '@/utils/libs/jssdk'
Vue.prototype.base_name = http.base_name;
Vue.prototype.$axios = axios;
Vue.prototype.$http = http;
Vue.prototype.$http5g = http5g;
Vue.prototype.$httpdong = httpdong;
Vue.prototype.$jssdk = jssdk;
// Vue.prototype.front_http=http.front_http
import "@/vantui";
import {
  Stepper,
  Dialog,
  Icon,
  Overlay,
  Switch,
  Toast,
  Badge,
  ShareSheet,
  NoticeBar,
  Search,
  Popup,
} from "vant";
import VueClipBoard from "vue-clipboard2";
import { Calendar, CountDown, Step, Steps } from "vant";
import { PasswordInput, NumberKeyboard } from "vant";
function is_weixn() {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
}
Vue.use(PasswordInput);
Vue.use(NumberKeyboard);

Vue.use(Calendar);
Vue.use(Step);
Vue.use(Steps);
Vue.use(CountDown);
Vue.use(VueClipBoard);
Vue.use(Stepper);
Vue.use(Dialog);
Vue.use(Icon);
Vue.use(Overlay);
Vue.use(Switch);
Vue.use(Toast);
Vue.use(Badge);
Vue.use(ShareSheet);
Vue.use(NoticeBar);
Vue.use(Search);
Vue.use(Popup);
import "vant/lib/index.css";

Vue.config.productionTip = false;

let APP_ENV_CONFIG;

let alipay_app = "";
// if (process.env.VUE_APP_FLAG === "test") {
//   const Vconsole = require("vconsole");
//   const vconsole = new Vconsole();
//   Vue.use(vconsole);
//   APP_ENV_CONFIG = "wxf80deec18d1bdc19"; //
//   alipay_app = "2021003143620812"; //
// } else {
//   // APP_ENV_CONFIG = 'wxf80deec18d1bdc19'//
//   // alipay_app = '2021003143620812'//
//   const Vconsole = require("vconsole");
//   const vconsole = new Vconsole();
//   Vue.use(vconsole);
//   APP_ENV_CONFIG = "wxf80deec18d1bdc19"; //
//   alipay_app = "2021003143620812"; //
// }

Vue.filter("formatDate", function (date) {
  if (!date) {
    return "";
  }
  return date.replace("T", " ").substring(0, 16);
});

Vue.filter("cellphone", function (iphone) {
  if (!iphone) {
    return "";
  }
  return iphone.substring(0, 3) + "****" + iphone.substring(7);
});

Vue.filter("formatCardNumber", function (data) {
  return data.substring(data.length - 4);
});

Vue.filter("userRoleLevel", function (data) {
  let map = new Map([
    ["2-0", "会员"],
    ["2-1584", "VIP"],
    ["2-80", "社长"],
    ["2-81", "合伙人"],
    ["2-82", "联合创始人"],
    ["2-1741", "经理"],
    ["2-1740", "经销商"],
    ["2-1742", "经销商"],
    ["2-116", "平台方"],
  ]);
  return map.get(data);
});
Vue.filter("amountFilter", function (num) {
  return parseFloat(num).toFixed(2);
});

/*本地设备类型*/
let u = navigator.userAgent;

if (u.indexOf("Android") > -1 || u.indexOf("Adr") > -1) {
  localStorage.setItem("device", "android");
} else if (u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
  localStorage.setItem("device", "ios");
} else {
  localStorage.setItem("device", "pc");
}

window.weixin = {
  appId: APP_ENV_CONFIG,
  alipayApp: alipay_app,
  isAndroid: function () {
    return localStorage.getItem("device") == "android";
  },

  login: function () {
    location.href = location.href.split("#")[0] + "#/register_weixin";
  },
  getUserId: function () {
    let token = null;
    if (this.isAndroid() && window.JS_TROOPS) {
      token = window.JS_TROOPS.getLoginUserId();
      console.log(
        "getLoginUserId---",
        window.JS_TROOPS.getValue("getLoginUserId"),
        window.JS_TROOPS.getLoginUserId()
      );
    } else {
      token = localStorage.getItem("user_id");
    }
    return token;
  },
  loginMobile: function () {
    let token = null;
    if (this.isAndroid() && window.JS_TROOPS) {
      token = window.JS_TROOPS.getLoginPhone();
      console.log(
        "loginMobile---",
        window.JS_TROOPS.getValue("getLoginPhone"),
        window.JS_TROOPS.getLoginPhone()
      );
    } else {
      token = localStorage.getItem("loginMobile");
    }
    return token;
  },
  setItem(strName, value) {
    if (this.isAndroid() && window.JS_TROOPS) {
      window.JS_TROOPS.saveValue(strName, value);
    } else {
      return localStorage.setItem(strName, value);
    }
  },
  getItem(strName) {
    if (this.isAndroid() && window.JS_TROOPS) {
      window.JS_TROOPS.getValue(strName);
    } else {
      return localStorage.getItem(strName);
    }
  },
  setItemToken: function (token) {
    let token2 = JSON.stringify(token);
    if (this.isAndroid() && window.JS_TROOPS) {
      window.JS_TROOPS.saveValue("fttToken", token2);
    } else {
      return localStorage.setItem("fttToken", token2);
    }
  },
  getToken: function () {
    let token = null;

    if (this.isAndroid() && window.JS_TROOPS) {
      token = window.JS_TROOPS.getValue("fttToken");
    } else {
      token = localStorage.getItem("fttToken");
    }

    if (!token) {
      return false;
    }
    let obj = JSON.parse(token);
    return obj.token;
  },
  removeToken: function () {
    if (this.isAndroid() && window.JS_TROOPS) {
      window.JS_TROOPS.deleteValue("fttToken");
    } else {
      return localStorage.removeItem("fttToken");
    }
  },

  uploadIF(file, funBack) {
    var that = this;
    var filePath = file.file.name;
    var fileFormat = filePath.split(".")[1].toLowerCase();
    if (!fileFormat.match(/png|jpg|jpeg/)) {
      // this.$Message.info('上传错误，文件格式必须是png、jpg、jpeg')
      Toast("上传错误，文件格式必须是png、jpg、jpeg");
      return false;
    }
    let reader = new FileReader();
    let file2 = file.file;
    if (file2) {
      if (file2.size > 1000000) {
        let fileSize = parseFloat(
          parseInt(file2["size"] / 1024 / 1024)
        ).toFixed(2);
        let base64 = null;
        reader.readAsDataURL(file2);
        reader.onload = (e) => {
          let img = new Image();
          img.src = e.target.result;
          img.onload = function () {
            let w = this.width > 1440 ? 1440 : this.width,
              h =
                this.width > 1440
                  ? 1440 * (this.height / this.width)
                  : this.height;
            let canvas = document.createElement("canvas");
            let ctx = canvas.getContext("2d");
            canvas.setAttribute("width", w);
            canvas.setAttribute("height", h);
            ctx.drawImage(this, 0, 0, w, h);

            if (fileSize < 1) {
              base64 = canvas.toDataURL(file2.type, 1);
            } else if (fileSize > 1 && fileSize < 2) {
              base64 = canvas.toDataURL(file2.type, 0.8);
            } else {
              base64 = canvas.toDataURL(file2.type, 0.5);
            }

            let f = that.base64URLtoFile(base64, "t.jpg");
            funBack(f);
          };
        };
        return false;
      } else {
        reader.readAsDataURL(file2);
        reader.onload = (data) => {
          funBack(file2);
        };
        return false;
      }
    }
  },
  base64URLtoFile: function (base64URL, filename) {
    var arr = base64URL.split(","),
      mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]),
      n = bstr.length,
      u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
  },
};
console.log("isAndroid", window.weixin.isAndroid());
Vue.prototype.$wweixin = window.weixin;
Vue.prototype.$is_weixn = is_weixn();
console.log("======Vue.prototype.$is_weixn", Vue.prototype.$is_weixn);
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (weixin.isAndroid() && window.JS_TROOPS) {
    try {
      let version = window.JS_TROOPS.getAppVersionCode();
    } catch {
      if (from.path == "/upgradeTips") {
        return;
      }
    }
  }

  var tokenStr = window.weixin.getToken() || localStorage.getItem("token");
  console.log("tokenStr", tokenStr);
  if (to.query && to.query.share_uid) {
    localStorage.setItem("share_uid_weixin", to.query.share_uid);
  }
  if (to.query && to.query.shareurl) {
    localStorage.setItem("share_weixin_shareurl", to.query.shareurl);
  }
  if (tokenStr) {
    console.log("tokenStr--", to.name);
    next();
  } else if (to.path == "/register") {
    if (weixin.isAndroid() && window.JS_TROOPS) {
      weixin.JS_TROOPS.toReg();
    } else {
      next();
    }
  } else if (to.path == "/forgot" || to.name == "qrCodePay") {
    next();
  } else if (
    to.name == "freeDetails" ||
    to.name == "sharegoodDetails" ||
    to.name == "allshopgoods"
  ) {
    next();
  } else if (to.path == "/register_weixin") {
    next();
    return false;
  } else {
    console.log("to.path", to.path);
    if (to.path !== "/register_weixin") {
      if (Vue.prototype.$is_weixn) {
        console.log("=======11111sss");
        next("/register_weixin");
        return false;
      } else {
        if (weixin.isAndroid() && window.JS_TROOPS) {
          weixin.JS_TROOPS.toLogin();
        } else {
          next("/register_weixin");
          return false;
        }
      }
    } else {
      if (weixin.isAndroid() && window.JS_TROOPS) {
        weixin.JS_TROOPS.toLogin();
      } else {
        next();
      }
    }
  }

  // next()
});

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
